<?php
import( 'de.ceus-media.framework.neon.DefinitionAction' );
import( 'classes.models.Role' );
import( 'classes.models.RightObject' );
import( 'classes.models.RightAction' );
/**
 *	Actions for Roles.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_DefinitionAction
 *	@uses			Role
 *	@uses			RightObject
 *	@uses			RightAction
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		0.6
 */
/**
 *	Actions for Roles.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_DefinitionAction
 *	@uses			Role
 *	@uses			RightObject
 *	@uses			RightAction
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		0.6
 */
class RoleActions extends CMF_Krypton_DefinitionAction
{
	public function act()
	{
		$config		= $this->ref->get( "config" );
		$request	= $this->ref->get( "request" );

		$this->loadLanguage( 'role' );

		if( $role_id	= $request->get( 'role_id' ) )
		{
			if( $request->get( 'editRole' ) )
				$this->editRole();
			else if( $request->get( 'removeRole' ) )
				$this->removeRole();
			else if( $request->get( 'addRight' ) )
				$this->addRight();
			else if( $request->get( 'removeRight' ) )
				$this->removeRight();
			else if( $request->get( 'saveRights' ) )
				$this->setRights();
				
		}
		else if( $request->get( 'addRole' ) )
			$this->addRole();
	}

	/**
	 *	Adds a Role.
	 *	@access		private
	 *	@return		void
	 */
	private function addRole()
	{
		$request	= $this->ref->get( 'request' );
		if( $this->validateForm( 'role', 'addRole', 'role', 'add' ) )
		{
			$rolename = $request->get( 'add_rolename' );
			$role	= new Role();
			$roles	= $role->getAllData();
			$roles	= $roles[$role->getTableName()];
			foreach( $roles as $role )
				if( strtolower( $role['rolename'] ) == strtolower( $rolename ) )
					return $this->messenger->noteError( $this->words['role']['msg']['error3'], $rolename );
			$role	= new Role();
			$data	= array(
				"rolename"	=> $rolename,
				"description"	=> $request->get( 'add_description' ),
				);
			$role_id	= $role->addData( $data );
			$request->remove( 'add_rolename' );
			$request->remove( 'add_description' );
			$this->messenger->noteSuccess( $this->words['role']['msg']['success1'] );
		}
	}

	/**
	 *	Edits a Role.
	 *	@access		private
	 *	@return		void
	 */
	private function editRole()
	{
		$request	= $this->ref->get( 'request' );
		if( $role_id = $request->get( 'role_id' ) )
		{
			if( $this->validateForm( 'role', 'editRole', 'role', 'edit' ) )
			{
				$rolename = $request->get( 'edit_rolename' );
				$role	= new Role();
				$roles	= $role->getAllData();
				$roles	= $roles[$role->getTableName()];
				foreach( $roles as $role )
					if( $role['role_id'] != $role_id && strtolower( $role['rolename'] ) == strtolower( $rolename ) )
						return $this->messenger->noteError( $this->words['role']['msg']['error3'], $rolename );
				$data	= array(
					"rolename"	=> $rolename,
					"description"	=> $request->get( 'edit_description' ),
					);
				$role	= new Role( $role_id );
				$role->modifyData( $data );
				$this->messenger->noteSuccess( $this->words['role']['msg']['success2'] );
			}
		}
		else
			$this->messenger->noteError( $this->words['role']['msg']['error1'] );
	}

	/**
	 *	Removes a Role.
	 *	@access		private
	 *	@return		void
	 *	@todo		finish Implementation
	 */
	private function removeRole()
	{
		$request	=& $this->ref->get( 'request' );
		$role_id	= $request->get( 'role_id' );
		
		$userrole	= new UserRole();
		$userrole->focusForeign( 'role_id', $role_id );
		$userrole->deleteData();
		
		$role	= new Role( $role_id );
		$data	= $role->getData( false, true );
		if( count( $data ) )
		{
			$role->deleteData();
			$this->messenger->noteSuccess( $this->words['role']['msg']['success3'], $data['title'] );
			$request->remove( 'role_id' );
			$request->remove( 'removeRole' );
		}
		return;	

		print_m( $_POST );
		die();
/*		$request	=& $this->ref->get( 'request' );
		if( $request->get( 'rid' ) )
		{
			if( is_array( $rights = $request->get( 'rights' ) ) && count( $rights ) )
			{
				$roleright	= new RoleRight();
				foreach( $rights as $right )
				{
					$parts	= explode( "|", $right );
					$data	= array(
						"rid"		=> $rid,
						"object"	=> $parts[0],
						"action"	=> $parts[1],
					);
					$roleright->addData( $data );
					$this->messenger->noteSuccess( "Role Right '".$parts[0]."->".$parts[1]."' has been set." );
				}
			}
			else
				$this->messenger->noteError ( $lan['roles']['msg']['error4'] );
		}
*/	}
	
	private function setRights()
	{
		$request	=& $this->ref->get( 'request' );

		if( $role_id = $request->get( 'role_id' ) )
		{
			$object	=  new RightObject();
			$action	=  new RightAction();
			$objects	= $object->getObjects();
			$actions	= $action->getActions();

			$changes	= 0;
			$role	= new RoleRight();
			$right	= $request->get( 'right' );
			foreach( $objects as $object_id => $object )
			{
				foreach( $actions as $action_id => $action )
				{
					$old		= (int)$role->hasRightByID( $role_id, $object_id, $action_id );
					$new	= isset( $right[$object_id][$action_id] ) ? 1 : 0;
					if( $old != $new )
					{
						if( $old < $new )
						{
							$data	= array(
								"role_id"			=> $role_id,
								"rightobject_id"	=> $object_id,
								"rightaction_id"	=> $action_id,
							);
							$role->addData( $data );
						}
						else
						{
							$role->focusForeign( 'role_id', $role_id );
							$role->focusForeign( 'rightobject_id', $object_id );
							$role->focusForeign( 'rightaction_id', $action_id );
							$role->deleteData();
							$role->defocus();
						}
						$changes++;
					}
				}
			}
			if( $changes )
				$this->messenger->noteSuccess( $this->words['role']['msg']['success6'], $changes );
			else
				$this->messenger->noteError( $this->words['role']['msg']['error5'] );
		}
		else
			$this->messenger->noteError ( $this->words['role']['msg']['error1'] );
	}
}
?>
